home *** CD-ROM | disk | FTP | other *** search
Text File | 1993-01-30 | 3.6 KB | 166 lines | [TEXT/ALFA] |
- #=============================================================================
- # "Electric" C functions.
- #=============================================================================
-
- # First, define macros to bypass the electric braces.
- proc ordLeftBrace {} {
- insertText "\{"
- }
- bind {'['} <cs> ordLeftBrace
-
- proc ordRightBrace {} {
- insertText "\}"
- blink [matchIt "\}" [expr [getPos]-1]]
- }
- bind {']'} <cs> ordRightBrace
-
- proc quoteWord {} {
- backwardWord
- insertText "'"
- forwardWord
- insertText "'"
- }
- bind ''' <z> quoteWord
-
- # returns the indent string of the line named by 'pos'
- proc indentString pos {
- set start [lineStart $pos]
- set end [nextLineStart $pos]
- set text [getText $start $end]
- for {set i 0} {1} {incr i} {
- set c [string index $text $i]
- if {($c != "\ ") && ($c != "\t")} then {
- return [string range $text 0 [expr $i-1]]
- }
- }
- return
- }
-
-
- # Brace on new line, same indentation. Insert on another new line, indented in.
- # First, see if we are on new line.
- proc electricCLeft {} {
- global elecLBrace
- deleteText [getPos] [selEnd]
- if {$elecLBrace == "0"} then {
- insertText "\{"
- return
- }
- set pos [getPos]
- set start [lineStart $pos]
- set text [getText $start $pos]
-
- for {set i $start} {$i < $pos} {incr i} {
- set c [lookAt $i]
- if {($c != "\ ") && ($c != "\t")} then {
- set indentation [getText $start $i]
- insertText "\r" $indentation "\{\r" $indentation "\t"
- return
- }
- }
- set indentation [getText $start $pos]
- insertText "\{\r" $indentation "\t"
- }
- bind '\{' <s> electricCLeft
-
-
- # Brace on new line, immediate carriage return
- proc electricCRight {} {
- global elecRBrace
- deleteText [getPos] [selEnd]
- if {$elecRBrace == "0"} then {
- insertText "\}"
- catch {blink [matchIt "\}" [expr [getPos]-2]]}
- return
- }
- set pos [getPos]
- set start [lineStart $pos]
-
- if {[catch {matchIt "\}" [expr $pos-1]} matched]} {
- beep
- return
- }
- set text [getText [lineStart $matched] $matched]
- regexp {^[ ]*} $text indentation
- for {set i $start} {$i < $pos} {incr i} {
- set c [lookAt $i]
- if {($c != "\ ") && ($c != "\t")} then {
- insertText "\r" $indentation "\}\r" $indentation
- blink $matched
- return
- }
- }
- set text [set indentation]\}\r$indentation
- replaceText $start $pos $text
- goto [expr {$start + [string length $text]}]
- blink [matchIt "\}" [expr $start-2]]
- }
- bind '\}' <s> electricCRight
-
-
- # Brace on new line, immediate carriage return. We don't do our electric stuff
- # if we are in the middle of a for statement.
- proc electricCSemi {} {
- global electricSemi
- deleteText [getPos] [selEnd]
- if {$electricSemi == "0"} then {
- insertText ";"
- return
- }
- set pos [getPos]
- set start [lineStart $pos]
- set text [getText $start $pos]
-
- if {[string first "for" $text] != "-1"} {
- set lefts 0
- set rights 0
- set len [string length $text]
- for {set i 0} {$i < $len} {incr i} {
- case [string index $text $i] in {
- "(" { incr lefts }
- ")" { incr rights }
- }
- }
- global globs
- set globs [list $lefts $rights $len]
- if {$lefts != $rights} {
- insertText ";"
- return
- }
- }
-
- insertText ";\r" [indentString $pos]
- }
- bind '\;' electricCSemi
-
-
- # 'C' programming mode
- proc setCMode {} {
- changeMode "C"
- uplevel #0 {
- set elecLBrace 1
- set elecRBrace 1
- set electricSemi 1
- set wordWrap 0
- set funcExpr {^[^ \t\(#\r/@].*\(.*\)$}
- set sortedIsDefault 1
- set funcTitle "Func"
- }
- }
-
- proc setC++Mode {} {
- changeMode "C++"
- uplevel #0 {
- set elecLBrace 1
- set elecRBrace 1
- set electricSemi 1
- set wordWrap 0
- set funcExpr {^([^ \t\(#\r/@].*[ \t]+)?([A-Za-z0-9:~_]+)[ \t\r]*\(.*\)?$}
- set funcPar 2
- set funcTitle "Meth"
- set sortedIsDefault 1
- }
- }
-
-
-